package com.socool.sinofaith.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.socool.sinofaith.model.SifQuestionOptionsBean;
import com.socool.sinofaith.util.AndroidSQLiteOpenHelper;

public class SifQuestionOptionsDao {
	private AndroidSQLiteOpenHelper dbHelper;

	public SifQuestionOptionsDao(Context context) {
		dbHelper = new AndroidSQLiteOpenHelper(context);
	}

	// 插入记录
	public int insert(SifQuestionOptionsBean sifQuestionOptionsBean) {
		Log.i("SQLite", "----insert----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"insert into "
							+ SifQuestionOptionsBean.TABLENAME
							+ " (OP_ID,TaskQuestion_ID,OptionValue,IsDefault,JumpTo,Task_ID,RemindFlag,CreateTime,ModifyTime) values(?, ?, ?, ?, ?, ?, ?, ?, ?)",
					new Object[] { sifQuestionOptionsBean.getOP_ID(),
							sifQuestionOptionsBean.getTaskQuestion_ID(),
							sifQuestionOptionsBean.getOptionValue(),
							sifQuestionOptionsBean.getIsDefault(),
							sifQuestionOptionsBean.getJumpTo(),
							sifQuestionOptionsBean.getTask_ID(),
							sifQuestionOptionsBean.getRemindFlag(),
							sifQuestionOptionsBean.getCreateTime(),
							sifQuestionOptionsBean.getModifyTime()});
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 删除记录
	public int delete(SifQuestionOptionsBean sifQuestionOptionsBean) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifQuestionOptionsBean.TABLENAME
					+ " where OP_ID = ?",
					new Object[] { sifQuestionOptionsBean.getOP_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 根据taskId删除记录
	public int deleteByTaskId(String taskId) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifQuestionOptionsBean.TABLENAME
					+ " where Task_ID = ?", new Object[] { taskId });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 更新记录
	public int update(SifQuestionOptionsBean sifQuestionOptionsBean) {
		Log.i("SQLite", "----update----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"update "
							+ SifQuestionOptionsBean.TABLENAME
							+ " set TaskQuestion_ID=?, OptionValue=?, IsDefault=?, JumpTo=?, Task_ID=?, RemindFlag=?, CreateTime=?, ModifyTime=? where OP_ID=?",
					new Object[] { sifQuestionOptionsBean.getTaskQuestion_ID(),
							sifQuestionOptionsBean.getOptionValue(),
							sifQuestionOptionsBean.getIsDefault(),
							sifQuestionOptionsBean.getJumpTo(),
							sifQuestionOptionsBean.getTask_ID(),
							sifQuestionOptionsBean.getRemindFlag(),
							sifQuestionOptionsBean.getCreateTime(),
							sifQuestionOptionsBean.getModifyTime(),
							sifQuestionOptionsBean.getOP_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 查询记录
	public ArrayList<SifQuestionOptionsBean> query(String id) {
		Log.i("SQLite", "----query----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifQuestionOptionsBean sifQuestionOptionsBean;
		ArrayList<SifQuestionOptionsBean> list = new ArrayList<SifQuestionOptionsBean>();
		// 若fileId为null或""则查询所有记录
		if (id == null || id.equals("")) {
			cursor = db.rawQuery("select * from "
					+ SifQuestionOptionsBean.TABLENAME, null);
		} else {
			cursor = db.rawQuery("select * from "
					+ SifQuestionOptionsBean.TABLENAME + " where OP_ID=?",
					new String[] { id });
		}
		while (cursor.moveToNext()) {
			sifQuestionOptionsBean = new SifQuestionOptionsBean();
			sifQuestionOptionsBean.setOP_ID(cursor.getString(cursor
					.getColumnIndex("OP_ID")));
			sifQuestionOptionsBean.setTaskQuestion_ID(cursor.getString(cursor
					.getColumnIndex("TaskQuestion_ID")));
			sifQuestionOptionsBean.setOptionValue(cursor.getString(cursor
					.getColumnIndex("OptionValue")));
			sifQuestionOptionsBean.setIsDefault(cursor.getString(cursor
					.getColumnIndex("IsDefault")));
			sifQuestionOptionsBean.setJumpTo(cursor.getString(cursor
					.getColumnIndex("JumpTo")));
			sifQuestionOptionsBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifQuestionOptionsBean.setRemindFlag(cursor.getString(cursor
					.getColumnIndex("RemindFlag")));
			sifQuestionOptionsBean.setCreateTime(cursor.getString(cursor
					.getColumnIndex("CreateTime")));
			sifQuestionOptionsBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifQuestionOptionsBean.toString());
			list.add(sifQuestionOptionsBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}

	// 自定义查询记录
	public ArrayList<SifQuestionOptionsBean> queryBySql(String sql) {
		Log.i("SQLite", "----queryBySql----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifQuestionOptionsBean sifQuestionOptionsBean;
		ArrayList<SifQuestionOptionsBean> list = new ArrayList<SifQuestionOptionsBean>();
		cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			sifQuestionOptionsBean = new SifQuestionOptionsBean();
			sifQuestionOptionsBean.setOP_ID(cursor.getString(cursor
					.getColumnIndex("OP_ID")));
			sifQuestionOptionsBean.setTaskQuestion_ID(cursor.getString(cursor
					.getColumnIndex("TaskQuestion_ID")));
			sifQuestionOptionsBean.setOptionValue(cursor.getString(cursor
					.getColumnIndex("OptionValue")));
			sifQuestionOptionsBean.setIsDefault(cursor.getString(cursor
					.getColumnIndex("IsDefault")));
			sifQuestionOptionsBean.setJumpTo(cursor.getString(cursor
					.getColumnIndex("JumpTo")));
			sifQuestionOptionsBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifQuestionOptionsBean.setRemindFlag(cursor.getString(cursor
					.getColumnIndex("RemindFlag")));
			sifQuestionOptionsBean.setCreateTime(cursor.getString(cursor
					.getColumnIndex("CreateTime")));
			sifQuestionOptionsBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifQuestionOptionsBean.toString());
			list.add(sifQuestionOptionsBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}
}
